<?php
// 退款控制器
class Controller_Admin_Refund extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'refund';
        $this->_list_pagesize = 100;
        $this->_no_buttons = TRUE;
        $this->_do_refund  = FALSE;
        $this->_do_refund_data = array();
        parent::__construct($req, $res);

        $this->_list_where = array(
//            array('platform', '=', 'ALIPAY'),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

    }

	//首页
    public function action_index ()
    {
        $this->action_list();
    }

    public function action_list ($page = 1)
    {
        $filter = array();
        $query  = $this->request->query();

        $filter['startDay'] = Arr::get($query, 'startDay');
        $filter['stopDay']  = Arr::get($query, 'stopDay');
        $filter['orderNo']  = Arr::get($query, 'orderNo');
        $filter['status']   = Arr::get($query, 'status', -1);
        $filter['statusList'] = array(0 => '未提交', 1 => '成功', 2 => '失败');
        $this->_do_refund   = Arr::get($query, 'dorefund');

        if (empty($filter['startDay']))
            $filter['startDay'] = date("Y-m-d", time() - 86400 * 7);

        if (empty($filter['stopDay']))
            $filter['stopDay'] = date("Y-m-d", time());

        // list_where
        if ($filter['status'] >= 0)
            $this->_list_where[] = array('status', '=', $filter['status']);

        if (! empty($filter['orderNo']))
            $this->_list_where[] = array('orderNo', 'like', '%'.$filter['orderNo'].'%');

        if (! empty($filter['startDay']))
            $this->_list_where[] = array('created', '>=', strtotime($filter['startDay']));

        if (! empty($filter['stopDay']))
            $this->_list_where[] = array('created', '<', strtotime($filter['stopDay']) + 86400);

        $this->template->set('form_include_head_file', $this->_form->include_edit_file());
        $this->template->set('filter', $filter);

        parent::action_list($page);

        if ($this->_do_refund)
        {
            $this->doRefund();
            return;
        }
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    private function rebuild_post_data (& $post)
    {
        //
    }

    public function list_filter_data (& $rows)
    {
        $this->_do_refund_data = $rows;

        foreach ($rows as & $rs)
        {
            $rs['refundMoney'] = '￥'.number_format($rs['refundMoney']/ 100, 2);
            $rs['status']      = $this->buildStatusName($rs['status']);
            $rs['platform']    = $rs['platform'] == 'ALIPAY' ? '支付宝' : '微信';
        }
    }

    public function list_get_data ($offset, $limit)
    {
        return NULL;
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        unset($menus[0]);
        unset($menus[1]);
        unset($menus[2]);

        if ($row['status'] != '成功')
        {
            $menus[] = "<a href=\"/admin/refund/dorefund/{$row['id']}\">退款</a>";
        }
    }

    public function action_dorefund ($nid)
    {
        $refund = new Payment_Refund;
        $detail = $refund->detail($nid);
        $platform = Arr::get($detail, 'platform');
        $orderNo  = Arr::get($detail, 'orderNo');
        $out_trade_no = Arr::get($detail, 'out_trade_no');
        $total_fee    = Arr::get($detail, 'totalMoney');
        $refund_fee   = Arr::get($detail, 'refundMoney');
        $orderId      = Arr::get($detail, 'orderId');

        if ($platform == 'WEIXIN')
        {
            $weixin = new Payment_Weixin_Refund;
            if ($weixin->order($orderNo, $out_trade_no, $total_fee, $refund_fee))
            {
                // 退款成功
                $refund->setRefundSuccess($orderId);
            }
            else
            {
                //
            }
        }
        else
        {
            //
            $alipay = new Payment_Alipay_Refund;
            $transactionId = Arr::get($detail, 'transactionId');
            $moneyYuan     = $refund_fee / 100;
            $comment       = "哇吖订单退款";
            $request = array(
                array(
                    'transactionId' => $transactionId,
                    'moneyYuan'     => $moneyYuan,
                    'comment'       => $comment,
                ),
            );

            $html = $alipay->doRefund($request);
            echo $html;exit;

            // 此处会跳转到支付宝做有密退款
        }

        $referrer = $this->request->referrer();
        $this->request->redirect($referrer);
    }

    private function buildStatusName($id)
    {
        switch ($id)
        {
            case 1:
                return '成功';
                break;

            case 2:
                return '失败';
                break;

            case 0:
            default:
                return '未提交';
                break;
        }
    }
}
